#import "UITableView+Refresh.h"
#import "MJRefresh.h"
#import "MDRefreshHeader.h"
#import "GoogleRefreshHeader.h"

@implementation UITableView (Refresh)

@dynamic refreshDelegate;
@dynamic noDataView;
@dynamic errorView;
static char staticRefreshDelegate;
static char staticNoDataView;
static char staticErrorView;

/**
 *  设置无数据视图
 *
 *  @return return value description
 */
- (UIView *)setNoDataView{
    
    return nil;
}
/**
 *  设置网络视图
 *
 *  @return return value description
 */
- (UIView *)setNetErrorView{
    
    return nil;
}
/**
 *  初始化下拉刷新控件,可自定义更改
 *
 *  @return 下拉刷新控件对象
 */
- (MJRefreshHeader *)getHeader {
    

    //    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];

        MDRefreshHeader *header = [MDRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
//    GoogleRefreshHeader *header = [GoogleRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    return header;
}
/**
 *  初始化上拉加载控件, 可自定义更改
 *
 *  @return 上拉刷新控件对象
 */
- (MJRefreshFooter *)getFooter {
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    return footer;
}
/**
 *  添加刷新控件，UITableView实例化的时候使用
 */
- (void)addRefresh{
    [self addHeader];
}

- (void)addHeader {
    
    if (self.header) return;
    self.header = [self getHeader];
}
/**
 *  添加上拉加载更多控件
 */
- (void)addFooter {
    
    if (self.footer) return;
    self.footer = [self getFooter];
}
/**
 *  移除上拉加载更多
 */
- (void)removeFooter{
    self.footer = nil;
}
/**
 *  开始刷新加载数据
 */
- (void)beginRefreshing{
    [self.header beginRefreshing];
}
/**
 *  结束刷新
 */
- (void)endRefreshing{

    if (self.header.state == MJRefreshStateRefreshing) {
        [self.header endRefreshing];
    }
    if (self.footer.state == MJRefreshStateRefreshing) {
        [self.footer endRefreshing];
    }
    if (self.header.hidden == YES) {
        self.header.hidden = NO;
    }
}
/**
 *  加载新数据
 */
- (void)loadNewData{
    
    if (self.errorView) {
        [self.errorView removeFromSuperview];
    }
    if (self.noDataView) {
        [self.noDataView removeFromSuperview];
    }
    self.footer = nil;
    if ([self.refreshDelegate respondsToSelector:@selector(tableViewWillRefreshHeader:)]) {
        [self.refreshDelegate tableViewWillRefreshHeader:self];
    }
}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    self.header.hidden = YES;
    if ([self.refreshDelegate respondsToSelector:@selector(tableViewWillRefreshFooter:)]) {
        [self.refreshDelegate tableViewWillRefreshFooter:self];
    }
}
/**
 *  添加无数据视图
 */
- (void)addNoDataView{

    if (self.errorView) {
        [self removeErrorView];
    }
    if (!self.noDataView) {
        // 设置无数据视图
        self.noDataView = [self setNoDataView];
        [self addSubview:self.noDataView];
    }
}
/**
 *  移除无数据视图
 */
- (void)removeNoDataView{
    
    [self.errorView removeFromSuperview];
    [self.noDataView removeFromSuperview];
    self.errorView  = nil;
    self.noDataView = nil;
}
/**
 *  添加网络错误视图提醒
 */
- (void)addErrorView{
    
    if (self.noDataView)
        [self removeNoDataView];
    if (!self.errorView) {
        // 设置网络错误视图
        self.errorView = [self setNetErrorView];
        [self addSubview:self.errorView];
    }
}
/**
 *  移除网络错误视图
 */
- (void)removeErrorView{
    [self removeNoDataView];
}

- (void)resetFooterWithCountOfResponse:(NSInteger)responseCount
                               countOfdataArray:(NSInteger)count
                                           rows:(NSInteger)rows{

    if (count == 0) {
        [self addNoDataView];
    }
    if (responseCount < rows)
        [self removeFooter];
    else
        [self addFooter];
}

#pragma mark - Getter And Setter Methods
- (void)setRefreshDelegate:(id<RefreshDelegate>)refreshDelegate{
    
    [self willChangeValueForKey:@"staticRefreshDelegate"];
    objc_setAssociatedObject(self, &staticRefreshDelegate, refreshDelegate, OBJC_ASSOCIATION_ASSIGN);
    [self didChangeValueForKey:@"staticRefreshDelegate"];
    [self addRefresh];
}

- (id<RefreshDelegate>)refreshDelegate{
    return objc_getAssociatedObject(self, &staticRefreshDelegate);
}

- (void)setNoDataView:(UIImageView *)noDataView{
    
    [self willChangeValueForKey:@"staticNoDataView"];
    objc_setAssociatedObject(self, &staticNoDataView, noDataView, OBJC_ASSOCIATION_RETAIN);
    [self didChangeValueForKey:@"staticNoDataView"];
}

- (UIImageView *)noDataView{
    return objc_getAssociatedObject(self, &staticNoDataView);
}

- (void)setErrorView:(UIView *)errorView{
    
    [self willChangeValueForKey:@"staticErrorView"];
    objc_setAssociatedObject(self, &staticErrorView, errorView, OBJC_ASSOCIATION_RETAIN);
    [self didChangeValueForKey:@"staticErrorView"];
}

- (UIView *)errorView{
    return objc_getAssociatedObject(self, &staticErrorView);
}

/**
 *  动效
 */

@end
